﻿
$(function () {
        //初始化列表信息
        init();
    });

//初始化列表信息
function init(queryData) {
    $('#list').datagrid({
        title: '车辆预约审核 ',
        idField: 'ApplyID',
        fitColumns: 'true',
        autoRowHeight: true,
        singleSelect: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: '/CarApply/GetList',
        queryParams: queryData,    //参数为了多条件查询预留
        columns: [[
        { field: 'ck', checkbox: true },
        { field: 'ApplyID', title: '申请ID', hidden: true },
        { field: 'CarID', title: '车辆ID', width: 180, hidden: true },
        { field: 'CarNumber', title: '车牌号码', width: 180, hidden: true },
        { field: 'UseCarPersonID', title: '用车人', width: 180, hidden: true },
        { field: 'ApplyUserName', title: '用车人', width: 180 },
            
        { field: 'UserCarDepartmentID', title: '用车部门', width: 180, hidden: true },
        { field: 'WithCarNumber', title: '随车人数', width: 180, hidden: true },
        {
            field: 'ApplyStartTime', title: '预计出发时间', width: 180,
            formatter: function (value, row, index) {
                return formatDate(value, "yyyy-MM-dd hh:mm:00");
            }
        },
        {
            field: 'ApplyEndTime', title: '预计返回时间', width: 180,
            formatter: function (value, row, index) {
                return formatDate(value, "yyyy-MM-dd hh:mm:00");
            }
        },
        {
            field: 'CreateTime', title: '创建时间', width: 180,
            formatter: function (value, row, index) {
                return formatDate(value, "yyyy-MM-dd hh:mm:00");
            }
        },
        {
            field: 'ApprovalTime', title: '审批时间', width: 180,
            formatter: function (value, row, index) {
                return formatDate(value, "yyyy-MM-dd hh:mm:00");
            }
        },
        {
            field: 'Status', title: '状态', width: 180,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "审批中";
                }
                if (value == 1) {
                    return "审批通过";
                }
                if (value == 2) {
                    return "退回";
                }
                if (value == 3) {
                    return "取消";
                }
            }
        },
        { field: 'Destination', title: '目的地', width: 180, hidden: true },
        { field: 'ApplyMileage', title: '申请里程数', width: 180, hidden: true },
        { field: 'UseCarReason', title: '用车原因', width: 180, hidden: true },
        { field: 'IsNeedDriver', title: '是否需要司机', width: 180, hidden: true },
        { field: 'Driver', title: '司机', width: 180, hidden: true },
        { field: 'ApprovalUserID', title: '审批人', width: 180, hidden: true },
        { field: 'ApprovalRemark', title: '审批备注', width: 180, hidden: true },
			
        { field: 'IsWebMessage', title: '是否短消息提醒', width: 180, hidden: true },
        { field: 'IsSMS', title: '是否SMS短信提醒', width: 180, hidden: true },
        { field: 'IsEmail', title: '是否邮件提醒', width: 180, hidden: true },
        { field: 'CreateBy', title: '创建人', width: 180, hidden: true },
        { field: 'UpdateBy', title: '修改人', width: 180, hidden: true },
        { field: 'UpdateTime', title: '修改时间', width: 180, hidden: true },

        ]],
    });
}

function doSearch() {
    var status = $('#q_Status').combotree('getValue');
    var applyUserID = $('#q_UseCarPersonID').val();
    var applyDate = $('#ApplyDate').datebox('getValue');
    var startTime = $('#q_StartTime').timespinner('getValue');
    var endTime = $('#q_EndTime').timespinner('getValue');

    var queryData = {
        j_status: status,
        j_applyUserID: applyUserID,
        j_applyDate: applyDate,
        j_startTime: startTime,
        j_endTime: endTime
    };

    init(queryData);
}


//弹出审核窗口
function check() {
    var row = $('#list').datagrid('getSelected');
    alert(row["ApprovalRemark"]);
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '审核');
        if (row["ApprovalRemark"]!=null) {

            var editor = CKEDITOR.instances.editor4;
            editor.setData(row["ApprovalRemark"]);
        }
        $('#fm').form('load', row);

        url = '/CarApply/Check';
    }
}

//保存数据
function save() {

    var editor = CKEDITOR.instances.editor4;
    var text4 = CKEDITOR.tools.htmlEncode(editor.getData());
    $('#ApprovalRemark').val(text4);
    $('#fm').form('submit', {
        url: url,
        dataType: 'content', //这里修改为content  
        onSubmit: function () {
            return $(this).form('validate');
        },
        
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/CarApply/Delete', { id: rows.MeetingRoomApplyID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}

//获取用户列表
function GetUserList(queryData) {
    $('#userdlg').dialog('open').dialog('setTitle', '选择部门主管');
    $('#userlist').datagrid({
        title: '用户列表',
        idField: 'UserID',
        fitColumns: 'true',
        singleSelect: true,
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: '/User/GetList',
        queryParams: queryData,    //参数为了多条件查询预留
        columns: [[
            { field: 'ck', checkbox: true },
            { field: 'UserID', title: 'ID', hidden: true },
            { field: 'UserName', title: '账号', width: 180 },
            { field: 'EmployeeName', title: '姓名', width: 180 },
            { field: 'DepartmentID', title: '部门', width: 180 },
            { field: 'RoleID', title: '角色（职务）', width: 180 },
            { field: 'Telephone', title: '固定电话', width: 180 }
        ]],
    });
}

//选择部门主管
function SelectUser() {
    var rows = $("#userlist").datagrid("getSelections");
    if (rows.length == 1) {
        var userID = rows[0].UserID;
        var employeeName = rows[0].EmployeeName;
        $("#q_UseCarPersonID").val(userID);
        $("#q_UseCarPersonName").val(employeeName);
        $('#userdlg').dialog('close');
    }
    else {
        $.messager.alert("友情提示", "请选择申请人");
    }
}

//根据部门ID，用户名查询用户
function Search() {
    var depatmentID = $('#comb_Search_DepartmentParentID').combotree('getValue');
    var username = $('#userName').val();

    if (depatmentID == '请选择部门') {
        depatmentID = '';
    }
    if (username == '用户名称') {
        username = '';
    }
    var querData = {
        j_depatmentID: depatmentID,
        j_username: username
    };

    GetUserList(querData);
}
